<?php

namespace App\Console\Commands;

use App\Models\Administrator;
use Illuminate\Console\Command;
use Illuminate\Database\Seeder;
use App\Models\AdministratorRole;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Artisan;

class ApplicationInstallCommand extends Command
{
    /**
     * 命令行的名称及用法
     *
     * @var string
     */
    protected $signature = 'install {action} {--q}';

    /**
     * 命令行的概述。
     *
     * @var string
     */
    protected $description = 'Application install tools.';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 执行命令
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        if (!$action) {
            $this->warn('Please choice action.');

            return;
        }

        $method = 'action' . implode('', array_map('ucfirst', explode('_', $action)));
        if (!method_exists($this, $method)) {
            $this->warn('action not exists.');

            return;
        }

        return $this->{$method}();
    }

    public function actionAdministrator()
    {
        $super = AdministratorRole::whereSlug(config('jumofang.administrator.super_slug'))->first();
        if (!$super) {
            $this->warn('请先运行 [ php artisan install role ] 命令来初始化jumofang的管理员权限数据。');

            return;
        }

        $name = '超级管理员';
        $mobile = $this->ask('请输入手机号');
        if (!$mobile) {
            $this->warn('手机号不能空');

            return;
        }
        $mobileExists = Administrator::whereMobile($mobile)->exists();
        if ($mobileExists) {
            $this->warn('手机号已经存在');

            return;
        }

        $password = '';
        while ($password === '') {
            $password = $this->ask('请输入密码(默认：123456):', '123456');
        }

        $passwordRepeat = '';
        while ($passwordRepeat === '') {
            $passwordRepeat = $this->ask('请再输入一次(默认：123456):', '123456');
        }

        if ($passwordRepeat !== $password) {
            $this->warn('两次输入密码不一致.');

            return;
        }

        $administrator = new Administrator([
            'name' => $name,
            'mobile' => $mobile,
            'password' => Hash::make($password),
        ]);
        $administrator->save();
        $administrator->roles()->attach($super->id);

        $this->info('管理员初始化成功.');
    }

    public function actionDev()
    {
        Artisan::call('migrate');
        Artisan::call('db:seed');
    }

    // 系统权限生成
    public function actionRole()
    {
        $seeder = new class() extends Seeder {
        };
        $seeder->call(\AdministratorSuperSeeder::class);
        $seeder->call(\AdministratorPermissionSeeder::class);
        $seeder->call(\AdministratorMenuSeeder::class);
        $this->info('权限数据初始化成功');
    }

    public function actionConfig()
    {
        $seeder = new class() extends Seeder {
        };
        $seeder->call(\AppConfigSeeder::class);

        $this->info('配置初始化完成');
    }
}
